# !/usr/bin/env python3
# -*- coding: utf-8 -*-
#
# @Time: 2021/3/24 19:11

import time
import schedule
from config.config import global_config
from log.logger import logger
from proxy.proxy import my_proxy
from crawler.query_weibo import query_dynamic
import utils.csv_util as csv_util 
from utils.util import *
from weather.weather import send_weather_to_feishu
from queue import Queue
from crawler.weibo_thread import WeiboThread

@catch_exceptions(cancel_on_failure=False)
def weibo_query():
    my_proxy.current_proxy_ip = my_proxy.get_proxy_from_list()
    uid_list = csv_util.get_weibo_uids()
    logger.info('开始检测:{}个用户,使用代理{}'.format(len(uid_list),my_proxy.current_proxy_ip))
    pageQueue = Queue(50)
    for i in range(len(uid_list)):
        if enable_dynamic_push == 'true':
            pageQueue.put(uid_list[i][0])
    crawl_threads = []
    crawl_name_list = ['weibo_crawl_1','weibo_crawl_2','weibo_crawl_3','weibo_crawl_4','weibo_crawl_5']
    for thread_id in crawl_name_list:
        thread = WeiboThread(thread_id,pageQueue)
        thread.start()
        crawl_threads.append(thread)

    while not pageQueue.empty(): 
        pass
    # 等待所有线程结束
    for t in crawl_threads:
        t.join()
    
    
def init_config():
    global intervals_second,enable_dynamic_push
    enable_dynamic_push = global_config.get_raw('config', 'enable_dynamic_push')
    intervals_second = global_config.get_raw('config', 'intervals_second')

if __name__ == '__main__':
    init_config()
    weibo_query()
    schedule.every(int(intervals_second)).seconds.do(weibo_query)
    schedule.every().day.at("09:30").do(send_weather_to_feishu)
    schedule.every(12).hours.do(my_proxy.init_proxys)
    while True:
        schedule.run_pending()
        time.sleep(1)